package com.techfort.metalrss.adapters;

import java.net.URL;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.techfort.metalrss.R;
import com.techfort.metalrss.ViewFeed;
import com.techfort.rssparser.Message;

public class ItemAdapter extends ArrayAdapter<Message>{

	private List<Message> messages;
	private Context context;
	public ItemAdapter(Context context, int textViewResourceId,
			List<Message> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		messages = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if(v==null){
			LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = li.inflate(R.layout.rssitem, null);
		}
		TextView title = (TextView) v.findViewById(R.id.title);
		title.setText(messages.get(position).getTitle());
		
		
		final URL url = messages.get(position).getLink();
		final String data = messages.get(position).getDescription();
		
		v.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent(context, ViewFeed.class);
				Bundle extras = new Bundle();
				extras.putString("DATA", data);
				intent.putExtras(extras);
				context.startActivity(intent);
			}
		});
		
		
		return v;
	}
}
